package com.gitee.huanminabc.fastboot.orm.methods.select;

import com.gitee.huanminabc.jlambda_sql.SqlWrapper;
import com.gitee.huanminabc.jlambda_sql.jdbc.JdbcCommand;
import com.gitee.huanminabc.fastboot.orm.methods.BaseMethod;

import java.lang.reflect.Method;

/**
 * 简要描述
 *
 * @Author: huanmin
 * @Date: 2025/4/6 15:15
 * @Version: 1.0
 * @Description: 文件作用详细描述....
 */
public class SelectCountMethod implements BaseMethod {
    @Override
    public String getSignature() {
        return "selectCount";
    }

    @Override
    public Object execute(Method method, Object[] args) {
        SqlWrapper sqlWrapper = (SqlWrapper) args[0];
        return selectCount(sqlWrapper.sql(), sqlWrapper.params());
    }
    //给sql套一层count
    public static String countSql(String sql) {
        return "SELECT COUNT(*) FROM (" + sql + ")  t";
    }
    public static long selectCount(String sql, Object[] args) {
        sql = countSql(sql);
        return (Long) JdbcCommand.selectFirstItem(sql, args);
    }
}
